<?php
require_once('Extension/Controller/Plugin/Abstract.php');

class Extension_Controller_Plugin_Common_View extends Extension_Controller_Plugin_Abstract
{
    //public function routeStartup(Zend_Controller_Request_Abstract $request) {}
    
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        
        self::initView($request);
    }

    //public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {}
    //public function preDispatch(Zend_Controller_Request_Abstract $request) {}
    //public function postDispatch(Zend_Controller_Request_Abstract $request) {}
    //public function dispatchLoopShutdown() {}


    public static function initView(Zend_Controller_Request_Abstract $request)
    {
        $moduleName = $request->getModuleName();

        if (!Zend_Registry::isregistered('view')) {

            $config = Zend::initConfig('View');

            if (null != $config->$moduleName) {

                $viewRenderer = Zend_Controller_Action_HelperBroker::getExistingHelper('viewRenderer');

                $viewRenderer->initView(PROJECT_ROOT . $config->$moduleName->viewBasePath);

                $host = $config->host;
                
                $view = $viewRenderer->view;

                $view->addHelperPath($config->helper->path, $config->helper->prefix)
                     ->setEncoding($config->encoding)
                     ->assign('host',               $host)
                     ->assign('charset',            $config->encoding)
                     ->assign('pathImages',         $host . $config->$moduleName->pathImages)
                     ->assign('pathJs',             $host . $config->$moduleName->pathJs)
                     ->assign('pathCss',            $host . $config->$moduleName->pathCss);

                Zend_Registry::set('view', $view);
                return $view;

            } else {
                throw new Zend_View_Exception("view setting for module $moduleName hasn't been made up yet. " 
                                            . "Please check your 'view.ini' file within your configurations.");
            }

        } else {
            return Zend_Registry::get('view');
        }
                
    }


}